package nfwx.analytics;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.util.UUID;

/**
 * 产生设备ID
 */

public class GrabDeviceId {
    /**
     * 产生设备ID
     * @return
     */
    static public String generateDeviceId(Context ctx)
    {
        String serial = android.os.Build.SERIAL;
        if (serial != null && serial.equals("") || serial.equals(Build.UNKNOWN)) {
            serial = null;
        }
        String telId=null;
        try {
            TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null) {
                telId = tm.getDeviceId();
            }
            if (telId != null && telId.equals("")) {
                telId = null;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            telId = null;
        }
        if (telId == null) {
            UUID uuid = UUID.randomUUID();
            return uuid.toString().replace("-","").toUpperCase();
        }
        if (serial != null) {
            telId += serial;
        }
        try {
            return GrabSend.MD5(telId);
        } catch(Exception e) {
            e.printStackTrace();
            UUID uuid = UUID.randomUUID();
            return uuid.toString().replace("-","").toUpperCase();
        }
    }
}
